package com.lyl.xxx.modules.web.account.controller;

import com.lyl.xxx.modules.framework.security.utils.SecurityUtil;
import com.lyl.xxx.modules.system.account.entity.SysUser;
import com.lyl.xxx.modules.system.account.service.SysUserService;
import com.lyl.xxx.modules.web.account.model.dto.LoginDTO;
import com.lyl.xxx.modules.web.account.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Mr.Luo
 * @date 2022-09-19 11:40
 * @desc 登录控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class LoginController {
    private final LoginService loginService;
    private final SysUserService sysUserService;

    /**
     * 登录
     *
     * @param dto 请求参数
     * @return token
     */
    @PostMapping("/login")
    public String login(@RequestBody @Valid LoginDTO dto) {
        return loginService.login(dto);
    }

    /**
     * 获取用户信息
     *
     * @return sysUser
     */
    @GetMapping("/info")
    public SysUser getUserInfo() {
        return sysUserService.getById(SecurityUtil.getUserId());
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public boolean logout() {
        return loginService.logout(SecurityUtil.getUserId(), SecurityUtil.getTokenKey());
    }
}
